/*
 * 创建日期 2009-12-15
 *
 * 成都天和软件公司
 * 电话：028-85425861
 * 传真：028-85425861-8008
 * 邮编：610041 
 * 版权所有
 */
package  org.fantasy.common.grid.custom;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import  org.fantasy.cpp.core.cache.ColumnDict;
import  org.fantasy.common.cache.util.CacheUtil;
import  org.fantasy.common.db.bean.Row;
import  org.fantasy.common.grid.bean.Column;
import  org.fantasy.common.grid.util.GridConst;
import  org.fantasy.common.util.ExpressionUtil;
import  org.fantasy.common.util.NumberUtil;
import  org.fantasy.common.util.StringUtil;

/**
 * 单元格基本模板
 * 
 * @author: 王文成
 * @version: 1.0
 * @since 2009-12-15
 */
public class BaseCellCustom extends AbstractCustom implements CellCustom {
	
	private ColumnDict dict;

	public String getClassName(Row row, Column col) throws Exception {
		return "";
	}

	public String getStyle(Row row, Column col) throws Exception {
		return "";
	}

	@Override
	public void setHttpServletRequest(HttpServletRequest request) {
		super.setHttpServletRequest(request);
        try {
			this.dict = (ColumnDict)CacheUtil.getCacheObject(request, "columnDictCache");
		} catch (Exception e) {
			throw new RuntimeException("数据字典缓存异常",e);
		}
	}

	public String getValue(Row row, Column col) throws Exception {
		String value = null;
		if (col.isDictType()) {// 从字典中查找
			String colValue = getColValue(row, col);
			value = dict.getName(col.getDict(), colValue);
		} else if (col.isValueType()) {
			value = parseFieldValue(row, col);
		} else if (col.isDetailType()) {
			value = "<span class='rdetail'></span>";
		} else if (col.isCheckboxType()) {
			String rowID = row.getString(GridConst.ROW_ID);
			value = "<input type='checkbox' name='" + rowID + "' class='" + GridConst.CHECKBOX_ROW + "'/>";
		} else if (col.isExpressionType()) {
			BigDecimal fieldValue = eval(col.getExpression(), row, col.getExpParams());
			int scale = col.isPercentType() ? 4 : 2;
			BigDecimal decimal = fieldValue.setScale(col.getScale(scale), BigDecimal.ROUND_HALF_UP);
			value = String.valueOf(decimal);
		} else {
			value = row.getCell(col.getFieldName()).getString();
		}

		if (col.isPercentType())
			return NumberUtil.getPercentNumber(value);
		else if (col.isCommaType())
			return NumberUtil.getCommaNumber(value);
		else
			return value;
	}

	/**
	 * 自定义表达式
	 * 
	 * @param expression
	 * @param row
	 * @param params
	 * @return
	 * @throws Exception
	 */
	protected BigDecimal eval(String expression, Row row, List<String> params) throws Exception {
		for (String param : params) {
			String value = row.getCell(param).getString();
			if (!StringUtil.isValid(value)) {
				value = "0";
			}
			expression = expression.replaceAll("#" + param + "#", "(" + value + "d)");
		}
		return ExpressionUtil.eval(expression);
	}

	/**
	 * 自定义内容
	 * 
	 * @param row
	 * @param col
	 * @return
	 * @throws Exception
	 */
	protected String parseFieldValue(Row row, Column col) throws Exception {
		String fieldValue = col.getFieldValue();
		String ctx = request.getContextPath();
		List<String> params = col.getValueParams();
		for (String param : params) {
			if (param.equals(Column.PARAM_CTX)) {
				fieldValue = fieldValue.replaceAll("#" + Column.PARAM_CTX + "#", ctx);
			} else {
				String value = row.getString(param);
				fieldValue = fieldValue.replaceAll("#" + param + "#", value);
			}
		}
		return fieldValue.trim();
	}

	/**
	 * 取得列值
	 * 
	 * @param row
	 * @param col
	 * @return
	 */
	private String getColValue(Row row, Column col) {
		String fieldName = col.getFieldName();
		return row.getString(fieldName);
	}
}
